package concurrent;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import support.ExcelOperator;
import util.MathUtil;

public class Test {
	private static void testsingleThread() {
		ExcelOperator eo = new ExcelOperator();
		double[][] data = eo
				.read("D:\\workspace\\CapacityInvestment\\data\\demand\\damand_10000_4.xlsx",
						"demand");
		List<Result> l = new ArrayList<Result>();
		long start = System.currentTimeMillis();
		for (int i = 0; i < data.length; i++) {
			CplexSolver cs = new CplexSolver(data[i], i);
			l.add(cs.execute());
		}
		long time = System.currentTimeMillis() - start;
		BigDecimal sum = BigDecimal.ZERO;
		for (Result r : l) {
			sum = sum.add(BigDecimal.valueOf(r.getResult()));
		}
		System.out.println("sum= " + sum + "; result size: " + l.size()
				+ " time: " + time);
	}

	private static void testConcurrent() {
		ExcelOperator eo = new ExcelOperator();
		double[][] data = eo
				.read("D:\\workspace\\CapacityInvestment\\data\\demand\\damand_10000_4.xlsx",
						"demand");
		Executer exe = new Executer(40, true);
		long start = System.currentTimeMillis();
		for (int i = 0; i < data.length; i++) {
			CplexSolver cs = new CplexSolver(data[i], i);
			exe.fork(cs);
		}
		List<Result> l = exe.join();
		long time = System.currentTimeMillis() - start;
		BigDecimal sum = BigDecimal.ZERO;
		for (Result r : l) {
			sum = sum.add(BigDecimal.valueOf(r.getResult()));
		}
		System.out.println("sum= " + sum + "; result size: " + l.size()
				+ " time: " + time);

		exe.shutdown();
	}

	public static void main(String[] args) {
		// double[] demand = { 1.0, 0.8, 0.9, 0.99 };
		// CplexSolver so = new CplexSolver(demand);
		// so.execute();

		testConcurrent();
		// testsingleThread();

		// ExcelOperator eo = new ExcelOperator();
		// double[] data = eo
		// .readSingle(
		// "D:\\workspace\\CapacityInvestment\\data\\demand\\concurrent.xlsx",
		// "data");
		// double ave = MathUtil.average(data);
		// System.out.println("ave = " + ave);
		//
		// Executer exe = new Executer(20, true);
		// long start = System.currentTimeMillis();
		// for (int i = 0; i < data.length; i++) {
		// exe.fork(new MyJob(data[i], ave));
		// }
		// List<Result> l = exe.join();
		// long time = System.currentTimeMillis() - start;
		// BigDecimal sum = BigDecimal.ZERO;
		// for (Result r : l) {
		// sum = sum.add(BigDecimal.valueOf(r.getResult()));
		// }
		// System.out.println("variance = "
		// + sum.divide(BigDecimal.valueOf(data.length), 5,
		// BigDecimal.ROUND_HALF_EVEN).doubleValue());
		//
		// System.out.println("result size: " + l.size() + " time: " + time);
		//
		// exe.shutdown();
	}
}
